/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.bubble;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JSlider;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import twaver.Element;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.chart.Bubble;
import twaver.chart.BubbleChart;
import demo.chart.Portlet;

public class CountryAreaChart extends Portlet {
	private TDataBox box = new TDataBox();
	private BubbleChart chart = new BubbleChart(box) {
		protected String getFormatedXScaleText(double value) {
			if (value > 180) {
				value = 180 - (value - 180);
				return super.getFormatedXScaleText(value) + "W";
			}
			if (value == 180) {
				return "E" + super.getFormatedXScaleText(value) + "W";
			}
			if (value < 0) {
				return super.getFormatedXScaleText(value) + "W";
			}
			return super.getFormatedXScaleText(value) + "E";
		}

		protected String getFormatedYScaleText(double value) {
			if (value < 0) {
				return super.getFormatedYScaleText(-value) + "S";
			}
			if (value == 0) {
				return "Equator";
			}
			if (value > 90) {
				return "";
			}
			return super.getFormatedYScaleText(value) + "N";
		}

		protected String getValueText(Element element, Bubble bubble, int index) {
			StringBuffer ret = new StringBuffer("<html>");
			ret.append(bubble.getText() + "<br>");
			ret.append("Area=" + bubble.getValue() + "<br>");
			ret.append("Density=" + element.getClientProperty("density"));
			return ret.append("</html>").toString();
		}

		protected Color getShapeColor(Element element, Bubble bubble, int index) {
			double density = ((Double) element.getClientProperty("density")).doubleValue();
			if (density < 10) {
				return Color.GREEN;
			}
			if (density > 100) {
				return Color.RED;
			}
			return Color.ORANGE;
		}

		protected double getShapeSize(Element element, Bubble bubble, int index) {
			return bubble.getValue() / 10000d * Math.min(chart.getDrawBound().width, chart.getDrawBound().height);
		}
	};

	public CountryAreaChart() {
		super.initialize(chart);
		chart.setShadowOffset(0);
		chart.setXGap(10);
		chart.setXScaleMaxValue(330);
		chart.setXScaleMinValue(-30);
		chart.setXScaleValueGap(60);
		chart.setYScaleMaxValue(120);
		chart.setYScaleMinValue(-90);
		chart.setYScaleValueGap(30);
		chart.setYScaleTextVisible(true);
		chart.setXAxisText("Longitude");
		chart.setYAxisText("Latitude");
		chart.setXScaleTextFont(new Font("dialog", Font.PLAIN, 10));
		chart.setYScaleTextFont(new Font("dialog", Font.PLAIN, 10));
		chart.setValueTextColor(Color.BLUE);
		chart.setValueTextFont(new Font("dialog", Font.ITALIC, 10));
		chart.getLegendPane().setVisible(false);
		chart.setBubbleStyle(TWaverConst.INFLEXION_STYLE_RECTANGLE);
		chart.setXAxisTextColor(Color.BLUE);
		chart.setXAxisTextFont(new Font("dialog", Font.ITALIC, 12));
		chart.setValueTextCenter(true);

		initElement("Russia", 50, 75, 1707, 9);
		initElement("Canada", 65 + 180, 65, 997, 3);
		initElement("China", 115, 30, 960, 126);
		initElement("America", 77 + 180, 25, 936, 30);
		initElement("Brazil", 50 + 180, -15, 854, 20);
		initElement("Australia", 149, -35, 774, 2);
		initElement("India", 45, 15, 328, 342);
		initElement("Argentina", 58 + 180, -42, 278, 13);
	}

	public void initElement(String name, double x, double y, double area, double density) {
		Element element = new Node();
		element.setName(name);
		Bubble bubble = new Bubble(name, x, y, area);
		element.addChartBubble(bubble);
		box.addElement(element);
		element.putClientProperty("density", new Double(density));
	}

	public JToolBar getControlPanel() {
		JToolBar toolbar = super.getControlPanel();
		final JSlider slider = new JSlider(-90, 90);
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				chart.setXAxisStartPosition(slider.getValue());
			}
		});
		slider.setValue(-90);
		toolbar.add(slider);
		return toolbar;
	}
}